package cz.farin.mandelinka.gui;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import cz.farin.mandelinka.Simulation;

public class BugWindow extends JFrame implements UserInterface {
	
	private Simulation sim;
	
	private MazeLabel mazeLabel;
	private ControlPanel controlPanel;
	
	
	public BugWindow() {
	}
	
	public void init() {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				initIntenal();			
			}						
		});
	}
	
	
	private void initIntenal() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout());
		
		mazeLabel = new MazeLabel();		
		add(mazeLabel, BorderLayout.CENTER);
		controlPanel = new ControlPanel(sim);
		add(controlPanel, BorderLayout.EAST);
		
		setTitle("Bugs");
		
		pack();
		setVisible(true);
	}
	
	
	
	//UserInterface methods
	
	@Override
	public void simulationStart(final Simulation sim) {
		this.sim = sim;
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				mazeLabel.setMaze(sim.getMaze());
				mazeLabel.setBugs(sim.getAliveBugs());				
				validateTree();
				pack();				
			}						
		});
	}

	@Override
	public void simulationOver() {		
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {							
				controlPanel.repaint();
			}						
		});
		System.out.println("Simulation over");
	}


	@Override
	public void stepOver() {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {				
				mazeLabel.repaint();	
				controlPanel.repaint();
			}						
		});
	}



}
